/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.appleframework.server;

import java.io.ByteArrayOutputStream;
import org.appleframework.Logger;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import org.appleframework.web.ActionInvoker;
import org.appleframework.web.HttpRequest;
import org.appleframework.web.HttpResponse;
import org.appleframework.web.Params;
import org.simpleframework.http.Form;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.http.core.Container;

/**
 *
 * @author zhujg
 */
public class HttpHandle implements Container {

  private static String server = "Apply Framework version 0.1";

  public void handle(Request req, Response res){
    HttpRequest request = null;
    HttpResponse response = null;
    try {
      OutputStream out = res.getOutputStream();
      long time = System.currentTimeMillis();
      response = new HttpResponse();
      response.out = new ByteArrayOutputStream();
      request = pasteRequest(req);
//      res.set("Content-Type", "text/plain");
//      res.set("Server", server);
//      res.setDate("Date", time);
//      res.setDate("Last-Modified", time);
      ActionInvoker.invoker(request, response);
      out.write(response.out.toByteArray());
      out.close();
      long end = System.currentTimeMillis();
      Logger.debug("共消耗 %s 毫秒.", String.valueOf((end - time)));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public HttpRequest pasteRequest(Request req) throws IOException {
//Cookie
//Connection
//Host
//Accept-Language
//Accept
//User-Agent
//Accept-Encoding
//UA-CPU
//    System.out.println(req.getValue("Connection"));
//    System.out.println(req.getValue("Host"));
//    System.out.println(req.getValue("Accept-Language"));
//    System.out.println(req.getValue("Accept"));
//    System.out.println(req.getValue("User-Agent"));
//    System.out.println(req.getValue("Accept-Encoding"));
//    System.out.println(req.getValue("UA-CPU"));
    HttpRequest request = new HttpRequest();
    request.host = req.getValue("host");
    request.path = req.getPath().getPath();
    request.method = req.getMethod().intern();
    request.ip = req.getClientAddress().getAddress().getHostAddress();
    if (request.host.contains(":")) {
      request.domain = request.host.split(":")[0];
      request.port = Integer.parseInt(request.host.split(":")[1]);
    } else {
      request.domain = request.host;
      request.port = 80;
    }
    Params params = new Params();
    Form form = req.getForm();
    Iterator<String> keySet = form.keySet().iterator();
    while (keySet.hasNext()) {
      String key = keySet.next();
      List<String> list = form.getAll(key);
      if (list.isEmpty()) {
        continue;
      }
      if (list.size() == 1) {
        params.put(key, form.get(key));
      } else {
        String[] values = list.toArray(new String[list.size()]);
        params.put(key, values);
      }
    }
    request.params = params;
    return request;
  }
}
